<?php
/**
 * 用于小伊开放平台的类
 * User: Jack
 * Date: 2019/6/12
 * Time: 8:18 PM
 */

namespace App\Utils;
use Hyperf\Utils\Codec\Json;
use Swoft\Bean\Annotation\Mapping\Inject;

class OpenService 
{
    
    /**
     * desc xiaoe开放平台生成签名
     *
     * @param  array  $params 参数数组
     * @param  string $mch_id 商户ID
     * @param  string $secret 商户密钥
     * @return array          带签名的参数
     */
    public static function getSign($params, $mch_id, $secret){
        
        $params['mch_id']    = $mch_id;
        $params['nonce_str'] = OpenService::getNonceStr();
        $params['timestamp'] = time();
        
        ksort($params);
        
        $str            = OpenService::FormatQueryParamMap($params);
        $str            = $str . '&key=' . $secret;
        $params['sign'] = strtoupper(md5($str));
        
        return $params;
    }
    
    /**
     * desc xiaoe开放平台验证签名
     *
     * @param  array  $params 参数数组
     * @param  string $secret 商户密钥
     * @return bool           是否通过验证
     */
    public static function verifySign($params, $secret){
        
        $sign = isset($params['sign']) ? $params['sign'] : '';
        unset($params['sign']);
        
        ksort($params);
        
        $str = OpenService::FormatQueryParamMap($params);
        $str = $str . '&key=' . $secret;
        
        // xeDebug(strtoupper(md5($str)));
        return $sign == strtoupper(md5($str));
    }
    
    /**
     * desc URL安全的base64编码
     *@param  $string string base64之后的字符串
     *@return string
     *@author zhougang
     */
    public static function urlSafeBase64Encode($string)
    {
        $data = str_replace(array('+', '/', '='),array('-', '_', ''), $string);
        return $data;
    }
    
    /**
     * desc URL安全的base64解码
     *@param  $string string url安全的base64编码之后的字符串
     *@return string
     *@author zhougang
     */
    public static function urlSafeBase64Decode($string)
    {
        $data = str_replace(array('-', '_'), array('+', '/'), $string);
        $mod4 = strlen($data) % 4;
        if($mod4)
        {
            $data .= substr('====', 4 - $mod4);
        }
        return $data;
    }
    
    public static function getNonceStr($length = 16)
    {
        $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $str = '';
        
        for($i = 0; $i < $length; $i++)
        {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
    
    public static function FormatQueryParamMap($paraMap)
    {
        
        $buff = '';
        ksort($paraMap);
        foreach ($paraMap as $k => $v)
        {
            if(is_array($v))
            {
                if(empty($v))
                {
                    $v = '';
                }
                else
                {
                    $v = json::encode($v, JSON_UNESCAPED_UNICODE);
                }
            }
            if($v != '')
            {
                $buff .= $k . '=' . $v . '&';
            }
        }
        $reqPar = '';
        if(strlen($buff) > 0)
        {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }
    
}